package com.orcs.leefei.utils.kotlin

import android.support.v7.widget.RecyclerView
import android.view.View

/**
 * Created by Administrator on 2017/2/21.
 */
fun RecyclerView.onItemClick(l: OnItemClickListener, ll: OnItemLongClickListener? = null) {
    OnRecyclerItemClickListener(this, l, ll)
}

fun RecyclerView.onItemClick(onItemClickListener: (recyclerView: RecyclerView, position: Int, view: View) -> Unit) {
    OnRecyclerItemClickListener(this, object : OnItemClickListener {
        override fun onItemClicked(rv: RecyclerView, i: Int, v: View) {
            onItemClickListener(rv, i, v)
        }

    }, null)
}

fun RecyclerView.onItemClick(highlightSelected: Boolean, onItemClickListener: (recyclerView: RecyclerView, position: Int, view: View) -> Unit) {
    OnRecyclerItemClickListener(this, object : OnItemClickListener {
        override fun onItemClicked(rv: RecyclerView, i: Int, v: View) {
            onItemClickListener(rv, i, v)
        }

    }, null, highlightSelected)
}

fun RecyclerView.onItemClick(highlightSelected: Boolean,defaultSelectedPosition:Int,  onItemClickListener: (recyclerView: RecyclerView, position: Int, view: View) -> Unit) {
    OnRecyclerItemClickListener(this, object : OnItemClickListener {
        override fun onItemClicked(rv: RecyclerView, i: Int, v: View) {
            onItemClickListener(rv, i, v)
        }

    }, null, highlightSelected, defaultSelectedPosition)
}

private class OnRecyclerItemClickListener(
        val recyclerView: RecyclerView,
        val onItemClickListener: OnItemClickListener?,
        val onItemLongClickListener: OnItemLongClickListener?,
        val highlightSelected: Boolean = false,
        var selectedPosition: Int = 0
) : RecyclerView.OnChildAttachStateChangeListener, View.OnClickListener, View.OnLongClickListener {

    override fun onChildViewDetachedFromWindow(view: View) {
        if (highlightSelected) {
            view.isSelected = false
        }
    }

    override fun onChildViewAttachedToWindow(view: View) {
        view.setOnClickListener(this)
        view.setOnLongClickListener(this)
        if (highlightSelected && (selectedPosition == recyclerView.getChildAdapterPosition(view))) {
            view.isSelected = true
        }
    }

    override fun onClick(view: View) {
        val adapterPosition = recyclerView.getChildAdapterPosition(view)
        if (adapterPosition != -1) {
            onItemClickListener?.onItemClicked(recyclerView, adapterPosition, view)
            if (highlightSelected) {
                selectedPosition = adapterPosition
                recyclerView.adapter.notifyDataSetChanged()
            }
        }
    }

    override fun onLongClick(v: View): Boolean {
        if (onItemLongClickListener != null) {
            val holder = recyclerView.getChildViewHolder(v)
            return onItemLongClickListener.onItemLongClicked(recyclerView, holder.adapterPosition, v)
        }
        return false
    }


    init {
        recyclerView.addOnChildAttachStateChangeListener(this)
    }
}

interface OnItemClickListener {
    fun onItemClicked(recyclerView: RecyclerView, position: Int, v: View)
}

interface OnItemLongClickListener {
    fun onItemLongClicked(recyclerView: RecyclerView, position: Int, v: View): Boolean
}